package android.fx.util;

import android.content.Context;
import android.fx.R;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Kalina on 2015/12/1.
 */
public class ToastUtils {
    private static Toast mToast = null;
    private static TextView tv_toast;

    private static void init(Context context) {
        if (mToast == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.toast_text, null);
            tv_toast = (TextView) view.findViewById(R.id.tv_toast_text);
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            wm.getDefaultDisplay().getMetrics(dm);
            int height = dm.heightPixels / 2;
            mToast = new Toast(context);
            mToast.setGravity(Gravity.CENTER, 0, height / 7 * 5);
            mToast.setView(view);
        }
    }

    public static void show(Context context, int textId, int duration) {
        show(context, context.getResources().getString(textId), duration);
    }

    public static void show(Context context, String text, int duration) {
        init(context);
        tv_toast.setText(text);
        mToast.setDuration(duration);
        mToast.show();
    }

    public static void showShort(Context context, int textId) {
        init(context);
        tv_toast.setText(context.getString(textId));
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.show();
    }

    public static void showShort(Context context, String text) {
        init(context);
        tv_toast.setText(text);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.show();
    }

    public static void showLong(Context context, String text) {
        init(context);
        tv_toast.setText(text);
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.show();
    }
}
